import sys
from odv_env import add_path_dll_directories
add_path_dll_directories()

from PySide2.QtWidgets import QApplication, QMainWindow,QWidget,QHBoxLayout,QVBoxLayout,QPushButton,QSpacerItem
from PySide2.QtCore import QTimer,Slot

from typing import Optional
from functools import partial

from pyodv import core as odvCore
from pyodv import gui as odvGui

class MainWindow(QMainWindow):
    odvViewer: odvCore.Viewer = None
    leftWidget: QWidget = None
    rightWidget: QWidget = None

    colorMapbtn: QPushButton = None

    def __init__(self):
        super().__init__()
        self.setWindowTitle("可视化")
        self.setupUi()

    def setOdvViewer(self, viewer:odvCore.Viewer)->None:
        self.odvViewer = viewer

    def setOdvWidget(self, widget:QWidget)->None:
        layout = QHBoxLayout(self.rightWidget)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(widget)
        
    @Slot()
    def addColorMap(self)->Optional[str]:
        self.odvViewer.setUseOsgModelViewAndProjectionUniforms(True)

        options = odvCore.ColorMapOptions()
        options.urls = ["/home/zhd/oceanDemo/data/daily_hats_2021_106.nc"]
        options.varNames = ["t"]

        options.dimOrder = odvCore.DataDimensionOrder.DepthLatLong
        options.depthRange = odvCore.DataRange(start=5.0,span=0.0)
        options.latRange  = odvCore.DataRange(start=-90.0,span=180.0)
        options.longRange  = odvCore.DataRange(start=0.0,span=360.0)

        options.colorBarOptions = odvCore.ColorBarOptions(
            colors={
                0.0: odvCore.Color(r=0.0, g=0.0, b=0.0),
                1.0: odvCore.Color(r=1.0, g=1.0, b=1.0)
            },
            minValue=20.0,
            maxValue=30.0
        )

        layerid = self.odvViewer.addLayer(options)
        return layerid

    def setupUi(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        # 创建水平布局
        layout = QHBoxLayout(central_widget)
        layout.setContentsMargins(0, 0, 0, 0)  # 去除默认边距

        # 菜单
        self.leftWidget = QWidget()
        self.leftWidget.setStyleSheet("background-color: #FFCCCC;")
        leftLayout = QVBoxLayout(self.leftWidget)

        self.colorMapbtn = QPushButton("添加色彩图")
        self.colorMapbtn.clicked.connect(self.addColorMap)
        leftLayout.addWidget(self.colorMapbtn)
        leftLayout.addStretch()
        
        # 可视化显示
        self.rightWidget = QWidget()
        self.rightWidget.setStyleSheet("background-color: #CCFFCC;")

        layout.addWidget(self.leftWidget, stretch=1)   # 左侧占比 1
        layout.addWidget(self.rightWidget, stretch=4)  # 右侧占比 4

if __name__ == "__main__":
    app = QApplication(sys.argv)

    mainWindow = MainWindow()
    mainWindow.resize(1920, 1080)  # 初始窗口大小
    mainWindow.show()

    viewerWidget = odvGui.ViewerWidget()
    odvWidget = viewerWidget.getQtWidget()
    odvViewer = viewerWidget.getViewer()

    mainWindow.setOdvViewer(odvViewer)
    mainWindow.setOdvWidget(odvWidget)

    sys.exit(app.exec_())